package cn.htsyb.synergy.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class ResourceUtil {
    private final static Logger logger = LoggerFactory.getLogger(ResourceUtil.class);

    public static String convertToLocalPath( String path) {
        String classPrefix = "classpath:";
        String filePrefix = "file:";
        String tagPath = path;
        if(path.isEmpty()){
            logger.warn("path：" + path + " 为空");
        }
        if (path.startsWith(classPrefix)) {
            tagPath = ClassLoader.getSystemResource("").getPath() + path.substring(classPrefix.length());
        } else if (path.startsWith(filePrefix)) {
            tagPath = path.substring(filePrefix.length());
        } else {
            tagPath = path;
        }
        //排除file中的偏差字符
        return new File(tagPath).getPath();
    }

    public static File getAsFile(String path) {

        return new File(convertToLocalPath(path));
    }

    public static InputStream getAsInputStream(String path) throws FileNotFoundException {

        return new FileInputStream(convertToLocalPath(path));
    }
}
